﻿var showProvincial='/report/status/province.do'   		 			//查询省级部门信息接口
var modifyState='/report/status/cancel/confirm.do';      			//修改上报状态为退回接口
var getReport='/forms/list.do'    					//获得报表信息接口


var auditState=4;			//需要上级部门审批的状态(申请退回状态)
var departIds='';
var page=1;
var pageSize=30;
var isEnd=false;

function loadForm(){
	$('#formid').combobox({
		 onSelect: function(rec){
			 query();
		 }
	});
}
	
function loadGRID(){
	//部门表格
	var iHeight = document.documentElement.clientHeight;
	$('#depart_Grid').datagrid({
		width: '100%',
		height: iHeight-80,
		toolbar:'#departBtn',
		fitColumns: true,
		striped: true,
		checkOnSelect:false,
		selectOnCheck:false,
		singleSelect:true,
		pagination:true,
        remoteSort:false,//表示前端排序不请求服务器
        columns:getShowColumns()
	});
}

//清空缓存
function clearDGRID(){
	$('#depart_Grid').datagrid({
		data:blankLine("depart_Grid",28)
	});
	var depart_pg = $("#depart_Grid").datagrid("getPager");
	$(depart_pg).pagination({
		total: 0,
		pageList: [30,50,100,500],
		beforePageText: '第',
		afterPageText: '页    共 ' + 0 + ' 页',
		displayMsg: '当前显示' + 0 + '-' + 0 + ' 条记录 共 ' + 0 + ' 条记录',
	});
}

function query(){

	var yearSelect=$("#yearSelect").combobox("getValue");
	var collectType=$("#collectType").combobox("getValue");
	var formid=$("#formid").combobox('getValue');   	//报表（妇儿、社会）
	// $.ajaxSettings.async = false;
	//加载省级部门信息方法
	$.get(showProvincial, {'year':yearSelect,'type':collectType,'formsid':formid,'page':page,'pageSize':50}, function (data) {
		if(data.code==200){
			var data  = data["data"].results;

			if(data==''||data==null||data==undefined||data=='[]'||data==[]){
				clearData();
				return;
			}
			load_jindutiao(data);
			var iHeight = document.documentElement.clientHeight;
			$('#depart_Grid').datagrid({
				width: '100%',
				height:iHeight-80,
				pagination:true,
				pageList: [30,50,100,500],
				data:data.slice(0,30),
				// onSelect:function(index, row){
				// 	// getTargetBydepart(row.departid,page,pageSize);
				// },
				onLoadSuccess:function(){
					isEnd=true;
				},
				onCheckAll:function(index, row){
					oncheckrows(index);
				},
				onCheck:function(index, row){
					oncheckrows(index);
				}
			});

			var pager = $("#depart_Grid").datagrid("getPager");
			pager.pagination({
				total:data.length,
				onSelectPage:function (pageNo, pageSize) {
					var start = (pageNo - 1) * pageSize;
					var end = start + pageSize;
					$("#depart_Grid").datagrid("loadData", data.slice(start, end));
					pager.pagination('refresh', {
						total:data.length,
						pageNumber:pageNo
					});
				}
			});

			$('#depart_Grid').datagrid('selectRow', 0);
		}else{
			loadGRID();
			clearDGRID();
			// clearTGRID();
		}
	}, "json");
}


$(function(){
	loadGRID();
	loadForm();
	loadYear();
});

//有小数位,最多4位
function isNumber(str){
	if(str==null||str.length<1){
		return false;
	}
	var reg= "^\\d+(\\.\\d{1,4})?$";
	var regExp = new RegExp(reg);
	return regExp.test(str); 
}

//修改部门上报状态(退回改为2，申请退回为4)
function modifyStates(departid,year){
	var formid=$("#formid").combobox("getValue");
	 $.ajax({
		 url:modifyState,
		 data:{'depart':departid,'year':year,"formsid":formid},
		 type: 'GET', 
		 dataType:"json",
		 contentType:"application/json;charset=UTF-8",
	     success: function (data) {
	           if(data.success){
	        	   	$.messager.alert('系统提示', '操作成功！','info');
	        	    query();
	           } 
	     },
	     error: function (xhr) {
	    	 	$.messager.alert('系统提示', '操作失败！','info');
	     }
	 });
}

//动态构建空白行JSON数据
function blankLine(grid,count){
	 var opts = $('#'+grid).datagrid('getColumnFields'); 
	 var data_s=[];
	 for (var i = 0; i < count; i++) {
		 var data={};
		 for (var j = 0; j < opts.length; j++) {
			 data[opts[j]]='';
		 }
		 data_s.push(data);
	 }
	 return data_s;
}
